﻿using System;
using System.Collections.Generic;
using System.Linq;
using yiCommerce.Core;
using yiCommerce.Core.Data;
using yiCommerce.Core.Domain.Customers;
using yiCommerce.Core.Domain.Statuses;
using yiCommerce.Core.Domain.Tags;
using yiCommerce.Service.Statuses;

namespace yiCommerce.Service.Tags
{
    public class TagService:ITagService
    {
        private IRepository<Tag> _tagRep;
        private IRepository<CustomerInterestedTag> _customerTagRep;

        public TagService(IRepository<Tag> tagRep, IRepository<CustomerInterestedTag> customerTagRep)
        {
            _tagRep = tagRep;
            _customerTagRep = customerTagRep;
        }

        public int AddTag(string tagName, string tagValue, int creatorId)
        {
            var query = from t in _tagRep.Table
                        where t.TagName == tagName && t.TagValue == tagValue
                        select t;
            var tag = query.FirstOrDefault();
            if (tag != null)
            {
                tag.CountTaged = tag.CountTaged + 1;
                return tag.Id;
            }
            else
            {
                var newTag = new Tag()
                    {
                        TagName = tagName,
                        TagValue = tagValue,
                        CreateAt = DateTime.Now,
                        CreatorId = creatorId,
                        Deleted = false,
                        CountTaged = 1,
                    };

                _tagRep.Insert(newTag);
                return newTag.Id;
            }
        }

        public IList<Tag> FindTags(string term)
        {
            var query = from t in _tagRep.Table
                       where t.TagName.Contains(term)
                       select t;
            return query.ToList();
        }

        public IPagedList<Tag> FindTags(int pageIndex, int pageSize)
        {
            var query = from c in _tagRep.Table
                        orderby c.CountTaged descending
                        select c;
            var tempResult = new PagedList<Tag>(query, pageIndex, pageSize);

            var list = tempResult.Select(c=>new Tag()
                        {
                            Id = c.Id,
                            CountTaged = c.CountTaged,
                            TagName = c.TagName,
                            TagValue = c.TagValue,
                            CreateAt = c.CreateAt,
                            CreatorId = c.CreatorId,
                            Deleted = c.Deleted,
                            TagIntroStatuses = FindStatusByTag(c.Id) 
                        }).ToList();

            var pagedList = new PagedList<Tag>(list, pageIndex, pageSize, query.Count());
            return pagedList;
        }


        public IList<TagIntroStatuses> FindStatusByTag(int tagId)
        {
            var query = from c in _tagRep.Table
                        from st in c.StatusTag
                        where st.TagId==tagId 
                        orderby st.Status.CreatedAt descending
                        select st.Status;


           
            return query.Take(3).ToList().Select(c=>new TagIntroStatuses()
                {
                    CreatedAt = c.CreatedAt,
                    CustomerId = c.CustomerId,
                    Id = c.Id,
                    Text = c.Text,
                    IsProductStatus = c.IsProductStatus,
                    Point = c.Point,
                    ProductId = c.ProductId
                }).ToList();
        }


        public void SaveInterestedTags(int customerId, IList<int> tagIdList)
        {

            var query = from cit in _customerTagRep.Table
                        where cit.CustomerId == customerId
                        
                        select cit;
            var citList = query.ToList();
            foreach (var customerInterestedTag in citList)
            {
                _customerTagRep.Delete(customerInterestedTag);
            }


            foreach (var tagid in tagIdList)
            {

                var customerTag = new CustomerInterestedTag()
                    {
                        CustomerId = customerId,
                        TagId = tagid,
                        DisplayOrder = 0,
                    };
                _customerTagRep.Insert(customerTag);
            }

        }

        public IPagedList<Tag> FindInterestedTags(int customerId, int pageIndex, int pageSize)
        {
            var query = from cit in _customerTagRep.Table
                        orderby cit.CreatedAt descending
                        where cit.CustomerId == customerId
                        select cit.Tag;

            var tempResult = new PagedList<Tag>(query, pageIndex, pageSize);

            var list = tempResult.Select(c => new Tag()
            {
                Id = c.Id,
                CountTaged = c.CountTaged,
                TagName = c.TagName,
                TagValue = c.TagValue,
                CreateAt = c.CreateAt,
                CreatorId = c.CreatorId,
                Deleted = c.Deleted

            }).ToList();

            var pagedList = new PagedList<Tag>(list, pageIndex, pageSize, query.Count());
            return pagedList;
        }
    }
}